<#include "/macro_.ftl">
INSERT
INTO
${entityMapper.tableName}
(<${r"#"}assign prefixName = true>
<#list entityMapper.fieldMapperMap?values as fieldMapper>
<${r"#"}if (insert_${fieldMapper.code}??) && (insert_${fieldMapper.code}_value??)>
${r"${prefixName?string('  ', ', ')}"}"${fieldMapper.columnName}"
<${r"#"}assign prefixName = false>
</${r"#"}if>
</#list>)
(SELECT
<${r"#"}assign prefixName = true>
<#list entityMapper.fieldMapperMap?values as fieldMapper>
<${r"#"}if (insert_${fieldMapper.code}??) && (insert_${fieldMapper.code}_value??)>
${r"${prefixName?string('  ', ', ')}"}:insert_${fieldMapper.code}_value
<${r"#"}assign prefixName = false>
</${r"#"}if>
</#list>
WHERE NOT EXISTS (SELECT 1 FROM ${entityMapper.tableName} E
<${r"#"}assign prefixName = true>
<#list entityMapper.fieldMapperMap?values as fieldMapper>
<${r"#"}if (where_and_eq_${fieldMapper.code}??)>
<${r"#"}if (where_and_eq_${fieldMapper.code}_value??)>
${r"${prefixName?string('WHERE ', 'AND ')}"}E."${fieldMapper.columnName}" = :where_and_eq_${fieldMapper.code}_value
<${r"#"}else>
${r"${prefixName?string('WHERE ', 'AND ')}"}<#if fieldMapper.typeCode == 'String'>(E."${fieldMapper.columnName}" IS NULL OR E."${fieldMapper.columnName}" = '')<#else>E."${fieldMapper.columnName}" IS NULL</#if>
</${r"#"}if>
<${r"#"}assign prefixName = false>
</${r"#"}if>
</#list>))